package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.KeyStore;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    //分类分页查询
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //1.开始分页查询--PageHelper
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        //2.调用Mapper执行分页查询-pageHelper固定的返回值Page<实体>
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);

        //3.获取pageSize里面的两个参数:total,records
        return new PageResult(page.getTotal(),page.getResult());
    }

    //修改分类
    @Override
    public void update(CategoryDTO categoryDTO) {
        //1.将DTO对象转成实体对象-对象属性拷贝-对象拷贝工具类BeanUtils
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        //2.补充基础属性-设置修改时间和修改人---使用@AutoFill注解集成了
        /*
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        */

        //3.调用Mapper操作数据库
        categoryMapper.update(category);
    }

    //新增分类
    @Override
    public void save(CategoryDTO categoryDTO) {
        //1.转成实体类，属性拷贝
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        //2.默认分类状态：禁用
        category.setStatus(StatusConstant.DISABLE);

        //3.补全基础属性---使用@AutoFill注解集成了

        /*
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        */

        //4.调用Mapper访问数据库
        categoryMapper.insert(category);
    }

    //根据id删除分类
    @Override
    public void deleteById(Long id) {
        //1.查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Integer count1 = dishMapper.countByCategoryId(id);
        if (count1 > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //2.查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        Integer count2 = setmealMapper.countByCategoryId(id);
        if (count2 > 0){
            //当前分类下有套餐，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //3.删除分类数据
        categoryMapper.deleteById(id);
    }

    //启用、禁用分类
    @Override
    public void startOrStop(Integer status, Long id) {
        //创建一个category对象--修改状态本质上是修改category表中的一个字段的属性值
        Category category = Category.builder()
                .id(id)
                .status(status)
                //.updateTime(LocalDateTime.now())
                //.updateUser(BaseContext.getCurrentId())
                .build();

        categoryMapper.update(category);
    }

    //根据类型查询分类
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }
}
